package wx.actions;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysConfigPayMemory;
import o2o.dao.PO.sys.SysConfigPay;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pay.config.WeixinConfig;
import wx.dao.AccessToken;
import wx.utils.WeixinUtil;

import java.util.HashMap;
import java.util.Map;

@Controller
public class WXUtilsAction {

    //获取code
    @RequestMapping(method = RequestMethod.POST, value = "/wx/accessToken/get")
    @ResponseBody
    public String getAccessToken() {
        //jinyouapp
//        // 第三方用户唯一凭证
////        String appId = "wx8fd1e45f0ffe1389";
//        String appId = "wxc79aa3d102477aa7";
//        // 第三方用户唯一凭证密钥
////        String appSecret = "f27d837b9c4cbf04578530d997302a02";
//        String appSecret = "b99e338dfaf68ba8941a70c9f964b32f";


        //boss数据通
        // 第三方用户唯一凭证
        String appId = "wx0a3fc42a0de29fa2";
        // 第三方用户唯一凭证密钥
        String appSecret = "7c72514c9aab0e8d701189b8698a5d22";

        // 调用接口获取access_token
        AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);

        return AjaxResult.successResult(at);
    }


    //微信code回调
    @RequestMapping(method = RequestMethod.GET, value = "/wx/openId/get")
    @ResponseBody
    public String getCodeResponse(String code, String state) {
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("state", state);
        //处理下一步
        System.out.println("微信code回调--code:" + code + ",state:" + state);

        //boss数据通
        String appId = "wx0a3fc42a0de29fa2";
        String appSecret = "7c72514c9aab0e8d701189b8698a5d22";

        return WeixinUtil.getUserOpenID(appId, appSecret, code);
//        String xml = null;
//        try {
//            xml = WeixinPayUtils.getInstance().winxin_pay_gzh(openId, state, 0.01f, 30, "商品", "商品详情");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (RepeatPayExcetion repeatPayExcetion) {
//            repeatPayExcetion.printStackTrace();
//        }
//        return xml;

//        return AjaxResult.successResult(map);
    }


    //微信 小程序code换取openId
    @RequestMapping(method = RequestMethod.POST, value = "/wx/little/openId/get")
    @ResponseBody
    public String getCodeOpenId(String sysAppKey, String appFlag, String code, String state,
                                Integer needSessionKey // 获取session_key和openId
    ) {
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("state", state);
        //处理下一步
        System.out.println("微信code回调--code:" + code + ",state:" + state);

        //小程序
        String appId = WeixinConfig.xcxAppId;
        String appSecret = WeixinConfig.xcxAppSecret;
        if (ValidateUtil.isNotNull(appFlag)) {
            SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByKey(sysAppKey, appFlag);
            if (null != sysConfigPay) {
                appId = sysConfigPay.getWxXcxAppId();
                appSecret = sysConfigPay.getWxXcxAppSecret();
            }
        }

        Boolean needSession = false;
        if (null != needSessionKey) {
            needSession = true;
        }

        String openId = WeixinUtil.getUserOpenIDLittle(appId, appSecret, code, needSession);
        return AjaxResult.successResult(openId);
    }
}
